/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package VIEW;

import CONTROLLERS.CategoryController;
import UTIL.InputValidator;
import VIEW.OLD.SecondhandTradeIn;
import static VIEW.ProductsUI.categoryController;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author RPhDsmbrana
 */
public class Category extends javax.swing.JPanel {

    /**
     * Creates new form Category
     */
    public Category() {
        
        try{
        initComponents();
        categoryController = new CategoryController(categoryTable, ProductsUI.getAddProductCategoryComboBox(),
                InventoryUI.getProductFilterCategoryComboBox(), DeliveryUI.getDeliveryProductFilterCategoryComboBox(), 
                TransferUI.getTransferProductFilterCategoryComboBox(), ProductsUI.getEditCategoryComboBox(), 
                ProductsUI.getProductFilterCategoryComboBox(), TradeToBrandNew.getCategoryComboBox(), SecondhandTradeIn.getCategoryEditComboBox(), 
                ViewSecondHandList.getCategoryComboBox(), AddToSecondHandList.getCategoryComboBox(), 
                TradeProduct.getCategoryComboBox(), ReplacementUI.getCategoryComboBox(), Reports.getCategoryComboBox(), Secondhand.getCategoryComboBox(),
                AdjustmentsNew.getFilterCategoryComboBox(), TradeProduct.getMultipleCategoryComboBox(), TradeToBrandNew.getMultipleCategoryComboBox(),
                Secondhand.getSecondhandCategoryComboBox(), categoryIDEditInput, categoryEditInput, editCategoryDialog);
                InputValidator.enabler(categoryTable, jButton2);
                InputValidator.enabler(categoryTable, jButton3);
                
                editCategoryDialog.setLocationRelativeTo(null);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        editCategoryDialog = new javax.swing.JDialog();
        jButton6 = new javax.swing.JButton();
        saveEditCategoryButton = new javax.swing.JButton();
        categoryEditInput = new javax.swing.JTextField();
        categoryIDEditInput = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        categoryInput = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        categoryTable = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        editCategoryDialog.setTitle("Edit Category");
        editCategoryDialog.setBounds(new java.awt.Rectangle(0, 0, 450, 200));
        editCategoryDialog.setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

        jButton6.setText("Cancel");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        saveEditCategoryButton.setText("Save");
        saveEditCategoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveEditCategoryButtonActionPerformed(evt);
            }
        });

        categoryEditInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                categoryEditInputKeyPressed(evt);
            }
        });

        categoryIDEditInput.setText("jLabel18");
        categoryIDEditInput.setEnabled(false);
        categoryIDEditInput.setVisible(false);

        jLabel17.setText("Category Name:");

        javax.swing.GroupLayout editCategoryDialogLayout = new javax.swing.GroupLayout(editCategoryDialog.getContentPane());
        editCategoryDialog.getContentPane().setLayout(editCategoryDialogLayout);
        editCategoryDialogLayout.setHorizontalGroup(
            editCategoryDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(editCategoryDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(editCategoryDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(editCategoryDialogLayout.createSequentialGroup()
                        .addComponent(saveEditCategoryButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6))
                    .addGroup(editCategoryDialogLayout.createSequentialGroup()
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(categoryEditInput, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(60, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, editCategoryDialogLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(categoryIDEditInput)
                .addContainerGap())
        );
        editCategoryDialogLayout.setVerticalGroup(
            editCategoryDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(editCategoryDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(categoryIDEditInput)
                .addGap(28, 28, 28)
                .addGroup(editCategoryDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(categoryEditInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(editCategoryDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveEditCategoryButton)
                    .addComponent(jButton6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Category Name:");

        categoryInput.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton1.setText("Add to Category List");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Category List");

        categoryTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "type_id", "Category Name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        categoryTable.setRowHeight(20);
        jScrollPane1.setViewportView(categoryTable);

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton2.setText("Edit Category");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton3.setText("Delete Category");
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Category");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addGap(56, 56, 56)
                        .addComponent(jButton3))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(categoryInput, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton1))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 728, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(96, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(categoryInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(39, 39, 39)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(64, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        editCategoryDialog.dispose();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void saveEditCategoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveEditCategoryButtonActionPerformed
        categoryController.editCategory();
    }//GEN-LAST:event_saveEditCategoryButtonActionPerformed

    private void categoryEditInputKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_categoryEditInputKeyPressed
        if (evt.getKeyCode()== KeyEvent.VK_ENTER)
        {
            saveEditCategoryButton.doClick();
        }
    }//GEN-LAST:event_categoryEditInputKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        categoryController.addCategory(categoryInput);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Integer selectedRow = categoryTable.getSelectedRow();
        String result = (String) categoryTable.getModel().getValueAt(selectedRow, 0);

        if(categoryController.openEditDialog(result, categoryEditInput, categoryIDEditInput)){
            editCategoryDialog.setVisible(true);
        }
        editCategoryDialog.setVisible(true);
        InputValidator.enabler(categoryTable, jButton2);
        InputValidator.enabler(categoryTable, jButton3);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        Integer selectedRow = categoryTable.getSelectedRow();
        String categoryName = (String) categoryTable.getModel().getValueAt(selectedRow, 1);
        int result = JOptionPane.showConfirmDialog(null, "Are you sure in deleting " + categoryName + " from the Category List","Warning!" ,JOptionPane.YES_NO_OPTION);
        String s = "";
        String name = categoryInput.getText();
        
            if(result == 0){
                categoryController.deleteCategory();
                categoryController.updateCategoryTable();
            }
        
        InputValidator.enabler(categoryTable, jButton2);
        InputValidator.enabler(categoryTable, jButton3);
    }//GEN-LAST:event_jButton3ActionPerformed

    public static void updateCategoryTable(){
        categoryController.updateCategoryTable();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField categoryEditInput;
    private javax.swing.JLabel categoryIDEditInput;
    private javax.swing.JTextField categoryInput;
    private javax.swing.JTable categoryTable;
    private javax.swing.JDialog editCategoryDialog;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton saveEditCategoryButton;
    // End of variables declaration//GEN-END:variables
}
